package com.agrm.rabc.service.impl;


import com.agrm.model.rabc.dto.RolePermMapping;
import com.agrm.rabc.mapper.RolePermMappingDao;
import com.agrm.rabc.service.PermService;
import com.agrm.rabc.service.RolePermMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


@CacheConfig(cacheNames = "c.s.r.service.impl.RolePermMappingServiceImpl")
@Service
public class RolePermMappingServiceImpl implements RolePermMappingService {
    private RolePermMappingDao rolePermMappingDao;
    private PermService permService;

    @Autowired
    public void setRolePermMappingDao(RolePermMappingDao rolePermMappingDao) {
        this.rolePermMappingDao = rolePermMappingDao;
    }

    @Autowired
    public void setPermService(PermService permService) {
        this.permService = permService;
    }

    @Cacheable(keyGenerator = "customKeyGenerator")
    @Override
    public List<RolePermMapping> findAll(RolePermMapping rpm) {
        List<RolePermMapping> mappings = rolePermMappingDao.findAll(rpm);
        mappings.forEach(it -> {
            it.setPerm(this.permService.findById(it.getPermId()));
        });
        return mappings;
    }

    @Cacheable(key = "#p0")
    @Override
    public RolePermMapping findById(Integer id) {
        RolePermMapping mapping = rolePermMappingDao.selectById(id);
        mapping.setPerm(this.permService.findById(mapping.getPermId()));
        return mapping;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(RolePermMapping rpm) {
        return rolePermMappingDao.insert(rpm) == 1;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(RolePermMapping rpm) {
        return rolePermMappingDao.updateById(rpm) == 1;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int deleteByIds(List<Integer> ids) {
        return rolePermMappingDao.deleteByIds(ids);
    }
}
